//
//  SetNicknameViewController.m
//  何五路
//
//  Created by _ADY on 15/7/29.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "SetNicknameViewController.h"

@interface SetNicknameViewController ()

@end

@implementation SetNicknameViewController
@synthesize fbDic;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"修改昵称";
        self.view.backgroundColor = bgColor;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;   // Do any additional setup after loading the view.
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 64, screenMySize.size.width, 50)];
    [textField setFont:[UIFont systemFontOfSize:labelSize]];
    textField.returnKeyType = UIReturnKeyDone;
    textField.delegate = self;
    textField.text = [NSString stringWithFormat:@"%@",[fbDic objectForKey:@"NickName"]];
    textField.textAlignment = 0;
    textField.tag = 10;
    textField.backgroundColor = [UIColor whiteColor];
    [textField setPlaceholder:@"请输入昵称"];
    [self.view addSubview:textField];
    
    UILabel *nLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 114, screenMySize.size.width, 30)];
    nLabel.text = @"4-20个字符，可由中英文、数字、\"_\"、\"-\"组成";
    nLabel.textColor = [UIColor blackColor];
    nLabel.font = [UIFont fontWithName:@"Arial" size:13];
    [self.view addSubview:nLabel];
    
}
- (BOOL)textField:(UITextField *)text1Field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *text = [text1Field.text stringByReplacingCharactersInRange:range withString:string];
    if (text.length > 20)
    {
        text1Field.text = [text substringToIndex:[text1Field.text length] - 1];
    }
//    else if (text.length < 4)
//    {
//        return NO;
//
//    }
    return YES;

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)rightAction
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    UITextField *tText = (UITextField *)[self.view viewWithTag:10];
    if (tText.text.length < 4) {
        [SendService alerViewMessage:@"昵称格式不正确，请重新输入！"];
        return;
    }
    [fbDic setObject:tText.text forKey:@"NickName"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService SaveInfo:fbDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (ret !=nil)
            {
                NSLog(@"ret %@",ret);
                
                if ([[ret objectForKey:@"Result"] isEqualToString:@"success"])
                {
                    
                    [self.delegate SetNicknameArray:fbDic];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[ret objectForKey:@"Message"]  delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alert show];
                }
                else
                    [SendService alerViewMessage:[ret objectForKey:@"Message"]];
            }
            
        });
    });
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
